/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package windowComponents;

import busEvents.AddEmailRecipient;
import ch.rakudave.suggest.JSuggestField;
import data.contact.Contact;
import data.contact.ContactFolder;
import data.email.EmailAddress;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import javax.mail.Message.RecipientType;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.apache.commons.lang3.StringUtils;
import sun.swing.SwingAccessor;
import window.NewEmailPanel;

/**
 *
 * @author Arcanis
 */
public class EmailInputField extends javax.swing.JPanel {

    private EmailAddress email;
    private RecipientType type = RecipientType.TO;
    private ContactFolder folder;
    private JSuggestField suggest;
    private NewEmailPanel panel = null;

    public EmailInputField(JFrame frame, AddEmailRecipient rec) {
        this(frame);
        suggest.setText(rec.address.getAddress());
        jComboBox1.setSelectedItem(rec.type);
        updateValues();
    }

    /**
     * Creates new form EmailInputField
     */
    public EmailInputField(JFrame frame) {
        initComponents();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement(RecipientType.TO.toString());
        model.addElement(RecipientType.CC.toString());
        model.addElement(RecipientType.BCC.toString());
        this.jComboBox1.setModel(model);

        Vector a = new Vector();
        a.add("neco");
        a.add("Dalsiho");

        removeAll();
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        add(jComboBox1, c);

        suggest = new JSuggestField(frame, a);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 2.0;
        c.gridx = 1;
        c.gridy = 0;
        add(suggest, c);



//        Dimension d = this.getPreferredSize();
//        d.width = 200;
//        setPreferredSize(d);


        jComboBox1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                updateValues();
            }
        });


        suggest.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {
                updateSuggests();
            }
        });

        suggest.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                updateSuggests();
            }
        });

        suggest.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateValues();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateValues();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateValues();
            }
        });

        updateValues();
    }

    private void updateValues() {
        email = new EmailAddress(suggest.getText().trim());
        if (jComboBox1.getSelectedItem().equals(RecipientType.TO.toString())) {
            type = RecipientType.TO;
        } else if (jComboBox1.getSelectedItem().equals(RecipientType.CC.toString())) {
            type = RecipientType.CC;
        } else {
            type = RecipientType.BCC;
        }
        if (panel != null) {
            suggest.requestFocusInWindow();
            panel.checkAndAddEmpty();
        }
    }
    
    public void setFocusToTextField(){
        suggest.requestFocus();
    }
    
    public boolean isTextFieldFocused(){
        return suggest.isFocusOwner();
    }

    public void registerValueChanged(NewEmailPanel panel) {
        this.panel = panel;
    }

    private void updateSuggests() {
        if (folder != null) {
            Vector a = new Vector();
            for (Contact cont : folder.getContacts()) {
                StringBuilder b = new StringBuilder();

                b.append(cont.getAddress().getAddress());
                if (!StringUtils.isEmpty(cont.getName()) || !StringUtils.isEmpty(cont.getSurname())) {
                    b.append(" (");
                    b.append(cont.getName().trim());
                    if (!StringUtils.isEmpty(cont.getSurname())) {
                        b.append(" ");
                    }
                    b.append(cont.getSurname().trim());
                    b.append(")");
                }

                a.add(b.toString());
            }
            suggest.setSuggestData(a);
        }
    }

    public void setContactFolder(ContactFolder folder) {
        this.folder = folder;
        updateSuggests();
    }

    public EmailAddress getValue() {
        return email;
    }

    public RecipientType getType() {
        return type;
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jComboBox1 = new javax.swing.JComboBox();

        setLayout(new java.awt.GridLayout(1, 2));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        add(jComboBox1);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration//GEN-END:variables
}
